package czk.concrent.waitandnotify;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/5/22 22:41
 */
public class TestWaitAndNotify {
    private static final Object object=new Object();
    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            synchronized (object){
                System.out.println("t1开始运行");
                try {
                    object.wait();
                    Thread.sleep(1*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"开始其他操作");
            }
        },"t1").start();
        new Thread(()->{
            synchronized (object){
                System.out.println("t2开始运行");
                try {
                    object.wait();
                    Thread.sleep(10*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"开始其他操作");
            }
        },"t2").start();
        Thread.sleep(100);
        synchronized (object){
            System.out.println("主线程开始唤醒了");
            // object.notify();
            // 每次只能唤醒一个,多个线程被同时唤醒时，只有一个线程可以成功的获取到锁，其他的线程进行继续等待。
            object.notifyAll();
        }
    }
}
